package com.dts.backproject.service.impl;

import com.dts.backproject.entity.po.Message;
import com.dts.backproject.exception.MyException;
import com.dts.backproject.mapper.MessageMapper;
import com.dts.backproject.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public List<Message> findAll() {
        List<Message> messages = messageMapper.selectList(null);
        return messages;
    }

    @Override
    public void saveMessage(String email, Message message) {
        if(message.getTitle() == null || message.getTitle().isEmpty()){
            throw new MyException("标题为空，快添加一个标题吧！");
        }
        if(message.getContent() == null || message.getContent().isEmpty()){
            throw new MyException("添加新的内容吧");
        }

        messageMapper.insert(message);
    }

    @Override
    public void deleteById(String id, String email) {
        Message message = messageMapper.selectById(id);
        if(message == null){
            throw new MyException("该帖子不存在！");
        }
        if(!message.getEmail().equals(email)){
            throw new MyException("非本用户帖子！");
        }
        messageMapper.deleteById(id);
    }
}
